package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.DishFlavor;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.PageDto;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.vo.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorService dishFlavorService;

    /**
     * 添加菜品和菜品的口味
     *
     * @return
     */
    @PostMapping
    public R add(@RequestBody DishDto dto) {

        return dishService.add(dto);
    }

    /**
     * 菜品信息分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R pageInfo(Integer page, Integer pageSize, String name) {
/*
        //构造分页构造器对象
        Page<Dish> pageInfo = new Page<>(page, pageSize);
        Page<DishDto> dishDtoPage = new Page<>();

        //条件构造器
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        queryWrapper.like(name != null, Dish::getName, name);
        //添加排序条件
        queryWrapper.orderByDesc(Dish::getUpdateTime);

        //执行分页查询
        dishService.page(pageInfo, queryWrapper);

        //对象拷贝
        BeanUtils.copyProperties(pageInfo, dishDtoPage, "records");

        List<Dish> records = pageInfo.getRecords();

        List<DishDto> list = records.stream().map((item) -> {
            DishDto dishDto = new DishDto();

            BeanUtils.copyProperties(item, dishDto);

            Long categoryId = item.getCategoryId();//分类id
            //根据id查询分类对象
            Category category = categoryService.getById(categoryId);
            String categoryName = category.getName();
            dishDto.setCategoryName(categoryName);

            return dishDto;
        }).collect(Collectors.toList());

        dishDtoPage.setRecords(list);

        return R.success(dishDtoPage);*/
        return dishService.pageInfo(page, pageSize, name);
    }

    /**
     * 修改菜品回显功能
     */
    @GetMapping("/{id}")
    public R findById(@PathVariable("id") Long id) {
        return dishService.findById(id);
    }

    /**
     * 修改菜品信息
     */
    @PutMapping
    public R updateDish(@RequestBody DishDto dto) {
        return dishService.updateDish(dto);
    }


    /**
     * 批量和单独修改菜品状态
     *
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    //这个参数这里一定记得加注解才能获取到参数，否则这里非常容易出问题
    public R<String> statusAll(@PathVariable("status") Integer status, @RequestParam List<Long> ids) {

        dishService.statusAll(status, ids);
        return R.success("更改成功");
    }

    /**
     * 批量和单独删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> deleteAll(@RequestParam("ids") List<Long> ids) {
        dishService.deleteAll(ids);
        return R.success("修改成功");
    }


    /**
     * 根据分类ID查询出菜品信息
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    public R listDishByCategoryId(Long categoryId) {
        //根据分类id查询分类下的所有菜品
        List<Dish> list = dishService.list(Wrappers.lambdaQuery(Dish.class)
                .eq(Dish::getCategoryId, categoryId)
                .eq(Dish::getStatus, 1));
        for (Dish dish : list) {
            //根据菜品id查询菜品的口味
            List<DishFlavor> flavors = dishFlavorService.list(Wrappers.lambdaQuery(DishFlavor.class).eq(DishFlavor::getDishId, dish.getId()));
            dish.setFlavors(flavors);
        }
        return R.success(list);
    }
}
